/**
 * 
 */
package com.demo.springmvc.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * @Desc
 *
 * @Author daijunjie
 * @DateTime 2016年6月20日 上午9:53:08
 * 
 */
public class Context implements Serializable {

	private static final long serialVersionUID = -3941721906773006371L;

	private Map<String, Object> param;

	public Context() {
		param = new HashMap<String, Object>();
	}

	public Map<String, Object> getParam() {
		return param;
	}

	public void setParam(Map<String, Object> param) {
		this.param = param;
	}

	public void clear() {
		param.clear();
	}

	public void putAll(Properties properties) {
		Iterator<Entry<Object, Object>> it = properties.entrySet().iterator();
		while (it.hasNext()) {
			Entry<Object, Object> entry = it.next();
			param.put((String) entry.getKey(), entry.getValue());
		}
	}

	public void putAll(Map<String, Object> map) {
		param.putAll(map);
	}

	public boolean containsKey(String key) {
		return param.containsKey(key);
	}

	public void set(String key, Object value) {
		param.put(key, value);
	}

	public Object get(String key, Object defaultValue) {
		Object result = param.get(key);
		if (result != null) {
			return result;
		}
		return defaultValue;
	}

	public Object get(String key) {
		return get(key, null);
	}

	public Boolean getBoolean(String key, Boolean defaultValue) {
		Object value = param.get(key);
		if (value != null) {
			return Boolean.parseBoolean(value.toString().trim());
		}
		return defaultValue;
	}

	public Boolean getBoolean(String key) {
		return getBoolean(key, null);
	}

	public Integer getInteger(String key, Integer defaultValue) {
		Object value = param.get(key);
		if (value != null) {
			return Integer.parseInt(value.toString().trim());
		}
		return defaultValue;
	}

	public Integer getInteger(String key) {
		return getInteger(key, null);
	}

	public Long getLong(String key, Long defaultValue) {
		Object value = param.get(key);
		if (value != null) {
			return Long.parseLong(value.toString().trim());
		}
		return defaultValue;
	}

	public Long getLong(String key) {
		return getLong(key, null);
	}

	public Double getDouble(String key, Double defaultValue) {
		Object value = param.get(key);
		if (value != null) {
			return Double.parseDouble(value.toString().trim());
		}
		return defaultValue;
	}

	public Double getDouble(String key) {
		return getDouble(key, null);
	}

	public String getString(String key, String defaultValue) {
		Object value = param.get(key);
		if (value != null) {
			return value.toString();
		}
		return defaultValue;
	}

	public String getString(String key) {
		return getString(key, null);
	}

	public Map<String, Object> getSubProperties(String prefix) {
		Map<String, Object> result = new HashMap<String, Object>();
		synchronized (param) {
			for (String key : param.keySet()) {
				if (key.startsWith(prefix)) {
					String name = key.substring(prefix.length());
					result.put(name, param.get(key));
				}
			}
		}
		return result;
	}

	@Override
	public String toString() {
		return "{ parameters:" + param + " }";
	}

}
